package com.cloudknow.healthyRun.activity.common;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.cloudknow.activity.BaseActivity;
import com.cloudknow.common.Constant;
import com.cloudknow.healthyRun.R;
import com.cloudknow.healthyRun.adapter.GuideViewPagerAdapter;
import com.cloudknow.utils.PrefsUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @date 创建时间 2017/10/19
 * @description 引导页
 */

public class GuideActivity extends BaseActivity implements ViewPager.OnTouchListener, View.OnClickListener {

    private String TAG =  getClass().getSimpleName();

    ViewPager vp_guide;
    TextView enterTv;

    private List<View> views;
    /**
     * 开始位置
     */
    float startX;
    /**
     * 结束位置
     */
    float endX;
    /**
     * 当前页面位置
     */
    private int currentPage = 0;

    private GuideViewPagerAdapter adapter;

    @Override
    protected int setView() {
        return R.layout.activity_introduction;
    }

    @Override
    protected void receiveData() {

    }

    @Override
    protected void initializeView() {

        getWindow().setBackgroundDrawable(null);

        // 全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        hideBottomUIMenu();
        initView();

        // 引导页图片资源
        int[] pics = { R.layout.view_splash_ad, R.layout.view_splash_ad1, R.layout.view_splash_ad2 };

        views = new ArrayList<View>();
        // 初始化引导页视图列表
        for (int i = 0; i < pics.length; i++) {
            View view = LayoutInflater.from(this).inflate(pics[i], null);
            // if (i == pics.length - 1) {
            // btn_enter.setTag(R.string.guide_tag_enter);
            // btn_enter.setOnClickListener(this);
            // }
            views.add(view);
        }

        adapter = new GuideViewPagerAdapter(views);
        vp_guide.setAdapter(adapter);
        vp_guide.setOnPageChangeListener(new PageChangeListener());
        vp_guide.setOnTouchListener(this);

        enterTv.setText(R.string.open_app);
        enterTv.setOnClickListener(this);

    }

    private void initView() {
        vp_guide = findViewById(R.id.vp_guide);
        enterTv = findViewById(R.id.enterTv);
    }

    @Override
    protected void loadDataFromServer() {

    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        // 隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            startX = event.getX();
            break;

        case MotionEvent.ACTION_UP:
            endX = event.getX();
            WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth();
            if (currentPage == views.size() - 1 && startX - endX >= (width / 5)) {
                // 设置引导页标识符并跳转
                enterMainActivity();
            }
            break;

        default:
            break;
        }
        return false;
    }

    private void enterMainActivity() {
        PrefsUtil.setInt(GuideActivity.this, Constant.isFirstInstall,2);
        openActivity(RoleChoiceActivity.class);

        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.enterTv:
            enterMainActivity();
            break;
        }
    }

    private class PageChangeListener implements ViewPager.OnPageChangeListener {
        /**
         * 当滑动状态改变时调用
         *
         * @param position
         */
        @Override
        public void onPageScrollStateChanged(int position) {
            // arg0 ==1的时辰默示正在滑动，arg0==2的时辰默示滑动完毕了，arg0==0的时辰默示什么都没做。

        }

        /**
         * 当前页面被滑动时调用
         *
         * @param position
         * @param arg1
         * @param arg2
         */
        @Override
        public void onPageScrolled(int position, float arg1, int arg2) {
            // arg0 :当前页面，及你点击滑动的页面
            // arg1:当前页面偏移的百分比
            // arg2:当前页面偏移的像素位置

        }

        /**
         * 当新的页面被选中时调用
         *
         * @param position
         */
        @Override
        public void onPageSelected(int position) {
            currentPage = position;

//            if (position == views.size() - 1) {
//                enterTv.setVisibility(View.VISIBLE);
//            } else {
//                enterTv.setVisibility(View.GONE);
//            }

            // if (position == 0) {
            // theFirstIv.setVisibility(View.VISIBLE);
            // the2Iv.setVisibility(View.GONE);
            // btn_enter.setVisibility(View.GONE);
            //
            // } else if (position == 1) {
            // theFirstIv.setVisibility(View.GONE);
            // the2Iv.setVisibility(View.VISIBLE);
            // btn_enter.setVisibility(View.GONE);
            // } else if (position == views.size() - 1) {
            // btn_enter.setVisibility(View.VISIBLE);
            // }
        }
    }
}
